package com.tanhua.server.controller;

import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author liuyp
 * @date 2021/01/26
 */
@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 我的-编辑个人资料-查询回显
     * GET /users
     */
    @GetMapping
    public ResponseEntity findById(Long userID, Long huanxinID) {
        Long id = null;
        if (userID != null) {
            id = userID;
        } else if (huanxinID != null) {
            id = huanxinID;
        } else {
            id = UserHolder.getUserId();
        }

        return userInfoService.findById(id);
    }

    /**
     * 我的-编辑个人资料-修改提交
     * PUT /users
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        return userInfoService.updateUserInfo(userInfo);
    }

    /**
     * 我的-通用设置-查询通用设置
     * GET /users/settings
     */
    @GetMapping("/settings")
    public ResponseEntity findSettings(){
        return userInfoService.findSettings();
    }

    /**
     * 我的-通用设置-查询黑名单列表
     * GET /users/blacklist
     */
    @GetMapping("/blacklist")
    public ResponseEntity findBlackList(int page, int pagesize){
        return userInfoService.findBlackList(page, pagesize);
    }

    /**
     * 我的-通用设置-移除黑名单
     * DELETE /users/blacklist/:uid
     */
    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity removeBlackList(@PathVariable("blackUserId") long blackUserId){
        return userInfoService.removeBlackList(blackUserId);
    }

    /**
     * 我的-通用设置-设置陌生人问题
     * POST /users/questions
     */
    @PostMapping("/questions")
    public ResponseEntity saveQuestion(@RequestBody Map map){
        String content = (String) map.get("content");
        return userInfoService.saveQuestion(content);
    }

    /**
     * 我的-通用设置-保存通知设置
     * POST /users/notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity saveSettings(@RequestBody Settings settings){
        return userInfoService.saveSettings(settings);
    }

    /**
     * 我的-统计 互相喜欢、喜欢、粉丝 数量
     * GET /users/counts
     */
    @GetMapping("/counts")
    public ResponseEntity queryLikeCount(){
        return userInfoService.queryLikeCount();
    }

    /**
     * 我的-查询 互相喜欢、喜欢、粉丝 列表
     * GET/users/friends/:type
     * type说明：
     *      值1：要查询  互相喜欢
     *      值2：要查询  喜欢的
     *      值3：要查询  粉丝
     *      值4：要查询  访客列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity queryLikeList(@PathVariable("type") Integer type,
                                        @RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pagesize,
                                        @RequestParam(required = false) String nickname){
        return userInfoService.queryLikeList(type, page, pagesize, nickname);
    }

    /**
     * 我的-粉丝-喜欢
     * POST /users/fans/:uid
     */
    @PostMapping("/fans/{id}")
    public ResponseEntity likeFans(@PathVariable("id") long targetId){
        return userInfoService.likeFans(targetId);
    }
}
